#include	"../network/CenterService.h"

#include	<protocols/Common.pb.h>
#include	<Script.h>
#include	<Logger.h>

namespace {

	int Broadcast(LuaStack & r) {
		std::string table = r.Get<std::string>(1);
		std::string func = r.Get<std::string>(2);
		std::string param = r.Get<std::string>(3);
		bool is_pvp = r.Get<bool>(4);

		Proto::CmdScript cmd;
		cmd.set_table(table);
		cmd.set_func(func);
		cmd.set_param(param);

		if (!is_pvp) {
			GService.SendAllGame(Proto::CMD_SCRIPT, cmd);
		}

		return 0;
	}

	int Call(LuaStack & r) {
		uint64_t server = r.Get<uint64_t>(1);
		std::string table = r.Get<std::string>(2);
		std::string func = r.Get<std::string>(3);
		std::string param = r.Get<std::string>(4);

		Proto::CmdScript cmd;
		cmd.set_table(table);
		cmd.set_func(func);
		cmd.set_param(param);

		Sender * sender = GService.Find(server);
		if (sender) sender->Reply(Proto::CMD_SCRIPT, cmd);
		return 0;
	}
}

void RegisterLuaApi_Net() {
	GLua.Register("Net")
		.Method("Broadcast", &Broadcast)
		.Method("Call", &Call);
}